package com.remist.service.impl;

import java.io.File;
import java.util.List;

import com.remist.common.Constant;
import com.remist.dao.ImagesDao;
import com.remist.model.ImageMapBean;
import com.remist.service.ImagesService;

public class ImagesServiceImpl implements ImagesService {
	private ImagesDao  imagesDao;

	public ImagesDao getImagesDao() {
		return imagesDao;
	}

	public void setImagesDao(ImagesDao imagesDao) {
		this.imagesDao = imagesDao;
	}

	public ImageMapBean addImage(ImageMapBean image) {
		return imagesDao.addImage(image);
	}

	public void deleteImageById(ImageMapBean image) {
		ImageMapBean mb = getImageById(image);
		String filePath =  Constant.getWEB_ROOT_PATH()+ mb.getImageUrl();
		File file = new File(filePath);
		if ( file.exists())
			file.delete();
		imagesDao.deleteImageById(image);
	}

	public ImageMapBean getImageById(ImageMapBean image) {
		return imagesDao.getImageById(image);
	}

	public List<ImageMapBean> getImagesByReferenceId(ImageMapBean image) {
		return imagesDao.getImagesByReferenceId(image);
	}

	public void deleteImageByReferenceId(ImageMapBean image) {
		imagesDao.deleteImageByReferenceId(image);
	}

	@Override
	public void updateImageRelativeProduct(ImageMapBean image) {
		ImageMapBean bean =  getImageById( image );
		bean.setReferenceId(image.getReferenceId());
		imagesDao.updateImage(bean);
	}

}
